﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DecoderRingLibrary
{
    public class Decoder
    {
        public int key { get; set; }


        public string Encode(string input)
        {
            var result = new StringBuilder();

            foreach (char c in input)
            {
                if (c >= 'A' && c <= 'Z')
                {
                    var i = (int)c;
                    i = i + key;
                    if(i > 90){ i = i - 26; }
                    var newC = (char)i;
                    result.Append(newC);
                }
                else if (c >= 'a' && c <= 'z')
                {
                    var i = (int)c;
                    i = i + key;
                    if (i > 122) { i = i - 26; }
                    var newC = (char)i;
                    result.Append(newC);
                }
                else
                {
                    result.Append(c);
                }
            }
            return result.ToString();
        }

        public string Decode(string input)
        {
            var result = new StringBuilder();

            foreach (char c in input)
            {
                if (c >= 'A' && c <= 'Z')
                {
                    var i = (int)c;
                    i = i - key;
                    if (i < 65) { i = i + 26; }
                    var newC = (char)i;
                    result.Append(newC);
                }
                else if (c >= 'a' && c <= 'z')
                {
                    var i = (int)c;
                    i = i - key;
                    if (i < 97) { i = i + 26; }
                    var newC = (char)i;
                    result.Append(newC);
                }
                else
                {
                    result.Append(c);
                }
            }
            return result.ToString();
        }
    }
}
